from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from flaskBlog.config import Config


db = SQLAlchemy()
bcrypt = Bcrypt()
mail = Mail()
loginManager = LoginManager()
loginManager.login_view = 'users.login'  # 路由的函数名
loginManager.login_message = '你需要登录才能继续访问.'
loginManager.login_message_category = 'info'


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    bcrypt.init_app(app)
    mail.init_app(app)
    loginManager.init_app(app)

    from flaskBlog.users.routes import users  # 不能放开头
    from flaskBlog.posts.routes import posts
    from flaskBlog.main.routes import main
    from flaskBlog.errors.handler import errors
    app.register_blueprint(users)
    app.register_blueprint(posts)
    app.register_blueprint(main)
    app.register_blueprint(errors)

    return app
